package info.techgeek.labs.java5Features.thread.blockingqueue;

import java.util.*;

public class Client {
	
   public static void main(String[] args) {
	   System.out.println("Client Start Main ");
      new Client().go();
   }

   public void go() {
	   System.out.println("Client Go ");
      final Server server = new Server();
      server.start();

      for (int i = 0; i < 5; i++) {
         final Request request = createRequest(i);
         server.accept(request,i);
      }
   }

   Request createRequest(final int index) {
	   System.out.println("Client index "+index);
      return new Request() {
         public void execute() {
        	 System.out.println("Client execute "+index);
            for (int i = 0; i <= 100; i += 10) {
            	int random = new Random().nextInt(5);
//            	System.out.println("Client random "+random);
            	int num =( random + 1) * 1000; 
//            	System.out.println("Client num "+num);
               sleep(num);
               System.out.println(String.format("request: %d completed: %d%%", index, i));
            }
            System.out.println(String.format("reqest %d completed", index));
         }

      };
   }

   private void sleep(int millis) {
	   System.out.println("Client sleep millis "+millis);
      try {
         Thread.sleep(millis);
      }
      catch (InterruptedException e) {

      }
   }
   
}

